$(function () {
	var user = {};
	user.loginId = getQueryString("loginId");
	user.mobile = getQueryString("mobile");
	user.username = decodeURI(getQueryString("username"));
	user.userType = getQueryString("userType");
	if (user.userType == "mentor") {
		vm.h5Header = "导师登录用户";
	} else if (user.userType == "parent"){
		vm.h5Header = "家长登录用户";
	} else {
		vm.h5Header = "秘书登录用户";
	}
	vm.user = user;
	vm.relationId = getQueryString("relationId");
	vm.getInfo(vm.user.loginId);
});

var vm = new Vue({
	el:'#mapp',
	data:{
		title: "编辑学生信息",
		h5Header: "",
		user: {},
		relationId: ""
	},
	methods: {
		saveOrUpdate: function (event) {
			if (StringUtils.isEmpty(vm.user.password)) {
				alert("密码不能为空!");
				return;
			}
			var thisUrl;
			if (StringUtils.isEmpty(vm.user.userId)) {
				thisUrl = ajaxServerUrlS("user/save");
			} else {
				thisUrl = ajaxServerUrlS("user/update");
			}
			console.log(thisUrl);
			$.ajax({
				type: "POST",
			    url: thisUrl,
                contentType: "application/json",
			    data: JSON.stringify(vm.user),
			    success: function(r){
			    	if(r.code === 0){
			    		//由于数据库传回来的是密文，在编辑状态下先保持明文。
			    		r.user.password = vm.user.password;
						vm.user = r.user;
						vm.saveLoginIdForRelatedEntiry();
					}else{
						alert(r.msg);
					}
				}
			});
		},
		saveLoginIdForRelatedEntiry: function() {
			var thisUrl;
			if (vm.user.userType == "mentor") {
				thisUrl = ajaxServerUrlG("bsnmentor/updateLoginId");
			} else {
				thisUrl = ajaxServerUrlG("bsnstudent/updateLoginId");
			}
			$.ajax({
				type: "POST",
			    url: thisUrl,
                contentType: "application/json",
			    data: JSON.stringify({
			    	id: vm.relationId,
			    	loginId:  vm.user.userId
			    }),
			    success: function(r){
			    	if(r.code === 0){
			    		alert("重置密码成功");
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getInfo: function(userId){
			console.log(userId);
			if (StringUtils.isEmpty(userId)) return;
			var thisUrl = ajaxServerUrlS("user/info/" + userId);
			$.get(thisUrl, function(r){
				console.log(r);
					if(r.code === 0){
						console.log(r.user);
						if (r.user) {
							r.user.password = "";
	                		vm.user = r.user;
	                		$("#in-pwd").attr("placeholder","已创建密码");
						}
					}else{
						alert(r.msg);
					} 
            });
		}
	}
});